package com.xwj.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xwj
 * @date 2020/5/9
 */
@Configuration
public class RabbitmqConfig {
    private static final Logger logger = LoggerFactory.getLogger(RabbitmqConfig.class);

    @Bean
    public RabbitTemplate rabbitTemplate(@Autowired CachingConnectionFactory factory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(factory);
        // 发送方确认回调函数
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if(ack) {
                logger.info("消息发送成功:correlationData({}),ack({}),cause({})", correlationData.getId(), ack, cause);
            } else {
                // 可以进行重新发送等操作
            }
        });
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            logger.info("消息丢失:exchange({}),route({}),replyCode({}),replyText({}),message:{}", exchange, routingKey, replyCode, replyText, message);
        });
        return rabbitTemplate;
    }

    /**
     * 订单死信队列
     *
     * @return
     */
    @Bean
    public Queue orderDeadQueue() {
        Map<String, Object> args = new HashMap<>(4);
        // 5秒过期
        args.put("x-message-ttl", 5000);
        // 死信交换机
        args.put("x-dead-letter-exchange", "order-dead-exchange");
        // 死信交换机路由键
        args.put("x-dead-letter-routing-key", "order-dead-routing-key");
        return new Queue("order-dead-queue", true, false, false, args);
    }

    /**
     * 交换机，面向生产者
     *
     * @return
     */
    @Bean
    public TopicExchange orderProducerExchange() {
        return new TopicExchange("order-producer-exchange", true, false);
    }

    /**
     * 订单死信队列与交换机绑定，面向生产者
     *
     * @return
     */
    @Bean
    public Binding basicProducerBinding() {
        return BindingBuilder.bind(orderDeadQueue()).to(orderProducerExchange()).with("order-producer-routing-key");
    }

    /**
     * 真正的消费队列，面向消费者
     *
     * @return
     */
    @Bean
    public Queue realOrderConsumerQueue() {
        return new Queue("order-consumer-queue", true, false, false);
    }

    /**
     * 死信交换机
     *
     * @return
     */
    @Bean
    public TopicExchange orderDeadExchange() {
        return new TopicExchange("order-dead-exchange", true, false);
    }

    /**
     * 死信交换机与实际消费队列绑定
     *
     * @return
     */
    @Bean
    public Binding orderDeadBinding() {
        return BindingBuilder.bind(realOrderConsumerQueue()).to(orderDeadExchange()).with("order-dead-routing-key");
    }
}
